package com.dfdchem.imd.kaiwu.activity.base;

import android.net.Uri;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.util.Log;
import com.dfdchem.imd.kaiwu.util.StringUtil;
import com.dfdchem.imd.kaiwu.util.UrlParse;

import java.util.Map;

@Route(path = RouterPath.SCHEME_FILTER_ACTIVITY)
public class SchemeFilterActivity extends BaseActivity {
    private static final String TAG = "SchemeFilterActivity";

    @Autowired
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Uri uri = getIntent().getData();
        Log.d(TAG, "uri:" + uri);
        Log.d(TAG, "uri getHost:" + uri.getHost());
        Log.d(TAG, "uri getScheme:" + uri.getScheme());
        Log.d(TAG, "uri getPath:" + uri.getPath());
        Log.d(TAG, "uri getQuery:" + uri.getQuery());
        Log.d(TAG, "url:" + url);

        String path = uri.getPath();
        String query = uri.getQuery();

        if (!StringUtil.isEmpty(url)) {
            ARouter.getInstance().build(RouterPath.WEB_VIEW_ACTIVITY)
                    .withString("url", url)
                    .withTransition(R.anim.right_push_in, R.anim.left_push_out)
                    .navigation(this);
            finish();
        } else {
            /**
             * 1、URL格式为 arouter://kaiwu/kaiwu/news?a=b&c=d 时，打开指定的Activity
             * 2、跳转到指定的网页 格式为：arouter://kaiwu/kaiwu/webView?url=http%3a%2f%2fwww.baidu.com
             * 3、
             */
            if (path != null && path.startsWith("/kaiwu")) {

                Map<String, String> params = UrlParse.getUriParams(uri);

                Postcard postcard = ARouter.getInstance().build(path);
                postcard.withTransition(R.anim.right_push_in, R.anim.left_push_out);

                for (String key : params.keySet()) {
                    postcard.withString(key, params.get(key));
                    Log.d(TAG, "postcard.withString key:" + key + " value:" + params.get(key));
                }
                postcard.navigation(this);
                finish();
            }
        }
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }
}
